﻿namespace PageTransition
{
    using System;
    using System.Windows;
    using System.Windows.Controls;

    using WP7Contrib.View.Controls.Transitions.Animation;
    using WP7Contrib.View.Controls.Transitions.Extensions;

    public partial class MainPage : AnimatedBasePage
    {
        private DialogService dialogService;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            AnimationContext = LayoutRoot;
        }

        protected override bool IsPopupOpen()
        {
            return this.dialogService != null ? this.dialogService.IsOpen : false;
        }

        protected override AnimatorHelperBase GetAnimation(AnimationType animationType, Uri toOrFrom)
        {

            //you could factor this into an intermediate base page to have soem other defaults
            //such as always continuuming to a pivot page or rultes based on the page, direction and where you are goign to/coming from

            if (toOrFrom != null)
            {
                if (toOrFrom.OriginalString.Contains("TurnstileFeather.xaml"))
                {
                    if (animationType == AnimationType.NavigateForwardOut)
                        return new TurnstileFeatherForwardOutAnimator() { ListBox = AnimationList, RootElement = LayoutRoot };
                    else
                        return new TurnstileFeatherBackwardInAnimator() { ListBox = AnimationList, RootElement = LayoutRoot };
                }
                else if (toOrFrom.OriginalString.Contains("Continuum.xaml"))
                {
                    return GetContinuumAnimation(AnimationList.ItemContainerGenerator.ContainerFromIndex(AnimationList.SelectedIndex) as FrameworkElement, animationType);
                }
                else if (toOrFrom.OriginalString.Contains("Slide.xaml"))
                {
                    //want to keep the page as is so return null
                    return null;
                }
            }

            return base.GetAnimation(animationType, toOrFrom);
        }

        protected override void AnimationsComplete(AnimationType animationType)
        {
            switch (animationType)
            {
                case AnimationType.NavigateForwardIn:
                    //Add code to set data context and bind data
                    //you really only need to do that on forward in. on backward in everything
                    //will be there that existed on forward out
                    break;

                case AnimationType.NavigateBackwardIn:
                    //reset list so you can select the same element again
                    AnimationList.SelectedIndex = -1;
                    break;
            }


            base.AnimationsComplete(animationType);
        }

        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (AnimationList.SelectedIndex != -1)
            {
                switch (AnimationList.SelectedIndex)
                {
                    case 0:
                        NavigationService.Navigate(new Uri("/Turnstile.xaml", UriKind.Relative));
                        break;

                    case 1:
                        NavigationService.Navigate(new Uri("/TurnstileFeather.xaml", UriKind.Relative));
                        break;

                    case 2:
                        NavigationService.Navigate(new Uri("/Continuum.xaml", UriKind.Relative));
                        break;

                    case 3:
                        //slide up
                        NavigationService.Navigate(new Uri("/Slide.xaml", UriKind.Relative));
                        break;

                    case 4:
                        //slide (popup)
                        this.dialogService = new DialogService() { Child = new SlideControl() };
                        this.dialogService.AnimationType = DialogService.AnimationTypes.Slide;
                        this.dialogService.Closed += OnDialogServiceClosed;
                        this.dialogService.Show();
                        break;


                    case 5:
                        this.dialogService = new DialogService() { Child = new SlideControl() };
                        this.dialogService.AnimationType = DialogService.AnimationTypes.Swivel;
                        this.dialogService.Closed += OnDialogServiceClosed;
                        this.dialogService.Show();
                        break;
                }
            }
        }

        void OnDialogServiceClosed(object sender, EventArgs e)
        {
            this.dialogService.Closed -= OnDialogServiceClosed;
            AnimationList.SelectedIndex = -1;
        }
    }
}